package com.cxria.script.upload.dao.impl

import com.cxria.script.upload.dao.IUploadDao
import com.cxria.script.upload.domain.Job
import sun.security.pkcs11.Session

import javax.management.Query;

class UploadDao extends BaseDao implements IUploadDao {

    @Override
    Job getJob(String jobId, String groupId, String type) {
        String hql = "From Job Where jobId = :jobId And groupId = :groupId And type = :type";
        Query<Job> query = getSession().createQuery(hql, Job.class);
        query.setParameter("jobId", jobId);
        query.setParameter("groupId", groupId);
        query.setParameter("type", type);
        return query.uniqueResult();
    }

    @Override
    void saveOrUpdate(Object... objects) {
        Session session = getSession();
        for (Object obj : objects) {
            session.saveOrUpdate(obj);
        }
    }

    @Override
    void deleteResource(String groupId) {
        String hql = "Update Resource Set isDelete = 1 Where groupId = :groupId";
        Query query = getSession().createQuery(hql);
        query.setParameter("groupId", groupId);
        query.executeUpdate();
    }

    @Override
    List<Job> getJobs(String groupId) {
        String hql = "From Job Where groupId = :groupId";
        Query<Job> query = getSession().createQuery(hql, Job.class);
        query.setParameter("groupId", groupId);
        return query.list();
    }

}
